#include "stdafx.h"
#include "ui/poke_hotkey_list_ctrl.h"
#include "models/atom_interface/ai_hotkey_manager.h"
#include "poke_framework.h"
#include "base/window/window_utils.h"

CPokeHotkeyListCtrl::CPokeHotkeyListCtrl()
{

}

CPokeHotkeyListCtrl::~CPokeHotkeyListCtrl()
{

}

BOOL CPokeHotkeyListCtrl::CanEdit(int nRow, int nCol)
{
    if(0 > nRow || 1 != nCol) {
        return FALSE;
    }

    return TRUE;
}

BOOL CPokeHotkeyListCtrl::OnRequestEdit(int nRow, int nCol, CRect oEditRect)
{
    if(0 > nRow) {
        return FALSE;
    }

    PokeHotkey oHotkey;
    CString strHotkeyDesc = GetItemText(nRow, 0);
    PokeFramework::get()->Execute(PAN_HOTKEY_MANAGER, PFI_GET_HOTKEY_BY_DESC, &strHotkeyDesc, &oHotkey);

    if(0 == oHotkey.m_oInfo.m_nHotkeyId) {
        return TRUE;
    }

    oEditRect.top -= 2;
    oEditRect.bottom += 2;
    m_oHotkeyFetcher.Create(WS_CHILD, oEditRect, this, 0);
    m_oHotkeyFetcher.SetFont(GetFont(), TRUE);
    m_oHotkeyFetcher.ResetOption(oHotkey.m_oInfo.m_nHotkeyId);
    m_oHotkeyFetcher.SetHotKey(oHotkey.m_oKey.m_nKey, SysHotkeyModifierToMfcHotkeyModifier(oHotkey.m_oKey.m_nModifier));
    m_oHotkeyFetcher.ShowWindow(SW_SHOW);
    m_oHotkeyFetcher.SetFocus();

    return TRUE;
}

BOOL CPokeHotkeyListCtrl::OnRequestDelete(int nRow)
{
    if(0 > nRow) {
        return FALSE;
    }

    PokeHotkey oHotkey;
    CString strHotkeyDesc = GetItemText(nRow, 0);
    PokeFramework::get()->Execute(PAN_HOTKEY_MANAGER, PFI_GET_HOTKEY_BY_DESC, &strHotkeyDesc, &oHotkey);

    if(oHotkey.m_oInfo.m_nHotkeyId == 0) {
        return FALSE;
    }

    PokeHotkeyKey oNewHotkey;
    PokeFramework::get()->Execute(PAN_HOTKEY_MANAGER, PFI_PUBLISH_HOTKEY, (void *)(LONG_PTR)oHotkey.m_oInfo.m_nHotkeyId, &oNewHotkey);
    PokeFramework::get()->FireEvent(PEI_UPDATE_HOTKEY_UI, NULL);

    SetSelectionMark(nRow);

    return FALSE;
}